package com.shj.em.biz.finance.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shj.em.biz.finance.entity.Receipt;
import com.shj.em.biz.finance.mapper.ReceiptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 开票信息 服务实现类
 *
 * @author Shen Huang Jian
 * @since 2018-07-14
 */
@Service
@Transactional
public class ReceiptServiceImpl extends ServiceImpl<ReceiptMapper, Receipt> implements ReceiptService {

    @Autowired
    ReceiptMapper receiptMapper;

    public List<Receipt> getReceiptsByOrderId(Long orderId){
        Wrapper<Receipt> wrapper = new EntityWrapper<>();
        wrapper.eq("order_id", orderId);
        wrapper.eq("del_flag", 0);
        return receiptMapper.selectList(wrapper);
    }

    public Page<Receipt> queryReceiptPage(Map<String, Object> params){
        int offset = (Integer)params.get("offset");
        int pageSize = (Integer)params.get("limit");
        int pageNo = offset / pageSize + 1;
        Page<Receipt> page = new Page(pageNo, pageSize);
        List<Receipt> list = receiptMapper.queryReceiptPage(page, params);
        return page.setRecords(list);
    }

    public String createReceipt(Receipt receipt){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderId", receipt.getOrderId());
        Map<String, BigDecimal> map = getTotalReceiptAmount(params);
        BigDecimal total = map.get("total");
        BigDecimal paid = map.get("paid");

        if(receipt.getAmount().add(paid).compareTo(total) > 0){
            return "错误！开票总金额大于订单总金额！";
        }
        receiptMapper.insert(receipt);
        return "";
    }

    public String updateReceipt(Receipt receipt){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderId", receipt.getOrderId());
        params.put("receiptId", receipt.getId());
        Map<String, BigDecimal> map = getTotalReceiptAmount(params);
        BigDecimal total = map.get("total");
        BigDecimal paid = map.get("paid");
        if(receipt.getAmount().add(paid).compareTo(total) > 0){
            return "错误！开票总金额大于订单总金额！";
        }

        receiptMapper.updateById(receipt);
        return "";
    }

    public Map<String, BigDecimal> getTotalReceiptAmount(Map<String, Object> params){
        Map<String, BigDecimal> map = receiptMapper.getTotalReceiptAmount(params);
        if(map.get("paid") == null){
            map.put("paid", new BigDecimal(0));
        }
        return map;
    }

    public boolean canAddReceipt(Long orderId){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderId", orderId);
        Map<String, BigDecimal> map = getTotalReceiptAmount(params);
        BigDecimal total = map.get("total");
        BigDecimal paid = map.get("paid");
        return total.compareTo(paid) == 1;
    }
}
